#!/bin/bash
# 修复 OpenCV 缓存问题，让 CocoaPods 使用已下载的文件

set -e

CACHE_DIR="$HOME/Library/Caches/CocoaPods/Pods/Release"
ZIP_FILE="$CACHE_DIR/opencv-4.1.0-ios-framework.zip"
COCOAPODS_CACHE="$HOME/Library/Caches/CocoaPods/Pods/External/OpenCV"

echo "🔧 修复 OpenCV 缓存问题..."
echo ""

# 检查缓存文件是否存在
if [ ! -f "$ZIP_FILE" ]; then
    echo "❌ 未找到 OpenCV zip 文件: $ZIP_FILE"
    echo "💡 请先运行 ./download_opencv.sh 下载文件"
    exit 1
fi

echo "✅ 找到 OpenCV zip 文件: $(du -h "$ZIP_FILE" | cut -f1)"
echo ""

# 创建 CocoaPods External 缓存目录
mkdir -p "$COCOAPODS_CACHE"

# 复制文件到 CocoaPods 期望的位置
echo "📦 复制文件到 CocoaPods 缓存目录..."
cp "$ZIP_FILE" "$COCOAPODS_CACHE/" 2>/dev/null || {
    echo "⚠️  直接复制失败，尝试创建符号链接..."
    ln -sf "$ZIP_FILE" "$COCOAPODS_CACHE/opencv-4.1.0-ios-framework.zip" 2>/dev/null || {
        echo "⚠️  符号链接也失败，尝试其他方法..."
    }
}

echo "✅ 缓存文件已准备"
echo ""
echo "💡 现在可以运行: pod install"
echo "💡 如果还是卡住，可能需要："
echo "   1. 清理 CocoaPods 缓存: pod cache clean OpenCV --all"
echo "   2. 或者手动解压文件到正确位置"
